package Study0;

public class Study03 {
    /*可变参数求最大值*/
    public static void printMax(double...add) {
        if(0 == add.length) {
            System.out.println("NO argument passed");
            return;
        }
        double tmp = add[0];
        for (int i = 0; i < add.length; i++) {
            if(tmp < add[i]) {
                tmp = add[i];
            }
        }
        System.out.println("Max ="+tmp);
    }
    public static void main (String[] args) {
        Study03 printMax = new Study03();
        Study03.printMax(1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0);
        Study03.printMax(9.0,8.0,7.0,6.0,5.0,4.0,3.0,2.0,1.0);
    }


    /*
    *//*Java 中的可变参数错误的示范 二*//*
    public static void demo3(int n,int...x) {
        for (int i = 0; i < n; i++) {
            System.out.println(x[i]);
        }
    }
    public static void main (String[] args) {
        Study3 demo3 = new Study3();
        Study3.demo3(6,1,2,3,4,5,6.6); // Java强类型语言，所传的参数类型不一致
    }
    */
    /*
    /*Java中的可变参数错误的示范 一/
    public static void demo2 (int ...x,int n) {
       可变参数必须为最后一个参数，


    }
    public static void main (String[] args) {

    }*/



    /*Java中的可变参数示范*/
    public static void demo1(int n,int...x) {
        for (int i = 0; i < n; i++) {
            System.out.println(x[i]);
        }
    }
    public static void main7(String[] args) {
        Study03 demo1 = new Study03();
        Study03.demo1(6,1,2,3,4,5,6);
    }


    /*Java 中的可变参数*/
    public static int test(int ...i) {
        for (int j = 0; j < 5; j++) {
            System.out.println(i[j]);
        }
        return 0;
    }
    public static void main6 (String[] args) {
        Study03 test = new Study03();
        Study03.test(1,2,3,4,5);

    }


    /*递归*/
    // 斐波那契数列
    public static int Fibonacci (int n) {
        if(1 == n || 2 == n) {
            return 1;
        }
        return Fibonacci(n-1)+Fibonacci(n-2);
    }
    public static void main5 (String[] args) {
        Study03 fibonacci = new Study03();
        System.out.println(Study03.Fibonacci(10));
    }
    /*循环实现斐波那契数列*/
    public static void main4 (String[] args) {
        int num1 = 1;
        int num2 = 1;
        for (int i = 1; i <= 10; i++) {
            if( 1 == i || 2 == i) {
                System.out.print(1+"\t");
                continue;
            }
            int sum = num1 + num2;
            num1 = num2;
            num2 =sum;
            System.out.print(sum+"\t");
        }
    }

    /*写一个递归，输入一个非负整数，放回组成它的数字之和，例如，输入 1729.侧应该返回 1+7+2+9 它的和为 19*/
    public static int sum(int n ) {
        int count = 0;
        if(n < 9) {
            return n;
        }
        return n%10+sum(n/10);
    }

    public static void main3 (String[] args) {
        Study03 sum = new Study03();
        System.out.println(sum(1729));
    }

    /*按一定顺序打印一个数字的每一位*/
    public static void print(int n) {
        if(n > 9) {
            print(n/10);
        }
        System.out.println(n%10);

    }
    public static void main2(String[] args) {
        Study03 print = new Study03();
        Study03.print(1234);
    }


    /*求阶乘*/
    public static int fac(int n ) {
        if(n == 1) {
            return 1;
        }
        return n*fac(n-1);
    }

    public static void main1 (String[] args) {
        int num = fac(3);
        System.out.println("num ="+num);
    }
}
